package com.molyfun.parents.activity.signup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.organization.OrganizationActivity;
import com.molyfun.parents.fragment.dialog.SignUpSeccessDialogFragment;
import com.molyfun.parents.rest.RestClient;
import com.molyfun.parents.rest.bean.SignUpEvaluateResponse;
import com.molyfun.parents.rest.bean.SignUpSubmitInfoRequest;
import com.molyfun.parents.rest.bean.SignUpSubmitInfoResponse;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpEvalucateSheetActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = "SignUpEvalucateSheetActivity";
    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @InjectView(R.id.submit_button)
    TextView mSubmitButton;
    private String departid;
    private SignUpEvaluateSheetAdapter evaluateSheetAdapter;
    private SignUpSeccessDialogFragment.Builder builder;
    private SignUpSeccessDialogFragment mSignUpSeccessDialogFragment;
    private String orderflag;
    public static final int REQUEST_CODE_CHANGE_ORGANIZATION = 2004;

    public static void startActivity(Context context, String departid, String studentName, String sex, String studentBornDate, String studentEmboryNum, String contryId, String studentIdentityCard,
                                     String reservationEmail, String resevationPhone, String reservationAddress, String motherName, String motherSex,String motherIdentity, String motherRelationship,
                                     String motherEdute, String motherProfession, String motherQQ, String motherConnect,
                                     String fatherName, String fatherSex,String fatherIdentity, String fatherRelationship, String fatherEdute, String fatherPression, String fatherQQ, String fatherConnect) {
        Intent intent = new Intent();
        //学生基本信息
        intent.putExtra("departid", departid);
        intent.putExtra("name", studentName);
        intent.putExtra("sex", sex);
        intent.putExtra("birthdatetime", studentBornDate);
        intent.putExtra("parity", studentEmboryNum);
        intent.putExtra("cityid", contryId);
        intent.putExtra("cardid", studentIdentityCard);

        intent.putExtra("email", reservationEmail);
        intent.putExtra("phone", resevationPhone);
        intent.putExtra("address", reservationAddress);
        intent.putExtra("mother_relationship", motherRelationship);
        intent.putExtra("mother_name", motherName);
        intent.putExtra("motherSex", motherSex);
        intent.putExtra("mother_cardid", motherIdentity);
        intent.putExtra("mother_education", motherEdute);
        intent.putExtra("mother_occupation", motherProfession);
        intent.putExtra("mother_qq", motherQQ);
        intent.putExtra("mother_phone", motherConnect);
        intent.putExtra("father_relationship", fatherRelationship);
        intent.putExtra("father_name", fatherName);
        intent.putExtra("fatherSex", fatherSex);
        intent.putExtra("father_cardid", fatherIdentity);
        intent.putExtra("father_education", fatherEdute);
        intent.putExtra("father_occupation", fatherPression);
        intent.putExtra("father_qq", fatherQQ);
        intent.putExtra("father_phone", fatherConnect);
        intent.setClass(context, SignUpEvalucateSheetActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_evaluate);
        ButterKnife.inject(this);
        initView();

    }

    private void initView() {
        getSupportActionBar().setTitle("");
        mTitle.setText("评测表");
        mSubmitButton.setVisibility(View.VISIBLE);
        mBack.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);
        departid = getIntent().getStringExtra("departid");

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        evaluateSheetAdapter = new SignUpEvaluateSheetAdapter(this);
        mRecyclerView.setAdapter(evaluateSheetAdapter);
        mSignUpSeccessDialogFragment = new SignUpSeccessDialogFragment(this);
        builder = new SignUpSeccessDialogFragment.Builder(this);
        loadData(departid);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;

            case R.id.submit_button:
                submitData();
                break;
        }
    }

    private void showSingleButtonDialog(String btnText,String count, View.OnClickListener onClickListener) {
        mSignUpSeccessDialogFragment = builder.setSingleButton(btnText, onClickListener).setMessage(count)
                .createSingleButtonDialog();
        mSignUpSeccessDialogFragment.show();
    }

    private void showHiteLineUpLayoutDialog(String btnText,   View.OnClickListener onClickListener) {
        mSignUpSeccessDialogFragment = builder.setSingleButton(btnText, onClickListener).hitLineUpLayout().createSingleButtonDialog();
        mSignUpSeccessDialogFragment.show();
    }

    private void loadData(String departid) {
        showProgressDialog();
        Call<SignUpEvaluateResponse> call = MFPApplication.getInstance().getRestClient().signUpEvaluate(String.valueOf(RestClient.DEFAULT_PAGE), String.valueOf(RestClient.LARGE_PAGE_SIZE), departid);
        call.enqueue(new Callback<SignUpEvaluateResponse>() {
            @Override
            public void onResponse(Call<SignUpEvaluateResponse> call, Response<SignUpEvaluateResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    List<SignUpEvaluateResponse.DataBean.SheetRecordsBean> records = response.body().getData().getRecords();
                    evaluateSheetAdapter.refreshData(response.body().getData().getRecords());
                }
            }

            @Override
            public void onFailure(Call<SignUpEvaluateResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

    private void submitData() {
        String student_name = getIntent().getStringExtra("name");
        String student_sex = getIntent().getStringExtra("sex");
        String student_birthdatetime = getIntent().getStringExtra("birthdatetime");
        String student_parity = getIntent().getStringExtra("parity");
        String student_cityid = getIntent().getStringExtra("cityid");
        String student_cardid = getIntent().getStringExtra("cardid");
        //预留信息
        String phone = getIntent().getStringExtra("phone");
        final String email = getIntent().getStringExtra("email");
        String address = getIntent().getStringExtra("address");


        String student_mother_relationship = getIntent().getStringExtra("mother_relationship");
        String mother_name = getIntent().getStringExtra("mother_name");
        String motherSex = getIntent().getStringExtra("motherSex");
        String mother_cardid = getIntent().getStringExtra("mother_cardid");
        String mother_education = getIntent().getStringExtra("mother_education");
        String mother_occupation = getIntent().getStringExtra("mother_occupation");
        String mother_qq = getIntent().getStringExtra("mother_qq");
        String mother_phone = getIntent().getStringExtra("mother_phone");

        String father_relationship = getIntent().getStringExtra("father_relationship");
        String father_name = getIntent().getStringExtra("father_name");
        String fatherSex = getIntent().getStringExtra("fatherSex");
        String father_cardid = getIntent().getStringExtra("father_cardid");
        String father_education = getIntent().getStringExtra("father_education");
        String father_occupation = getIntent().getStringExtra("father_occupation");
        String father_qq = getIntent().getStringExtra("father_qq");
        String father_phone = getIntent().getStringExtra("father_phone");
        List<SignUpSubmitInfoRequest.ContactlistBean> contactlistBeenList = new ArrayList<>();
        if (!TextUtils.isEmpty(student_mother_relationship)) {
            SignUpSubmitInfoRequest.ContactlistBean motherListBean = new SignUpSubmitInfoRequest.ContactlistBean();
            motherListBean.setName(mother_name);
            motherListBean.setSex(motherSex);
            motherListBean.setPhone(mother_phone);
            motherListBean.setCardid(mother_cardid);
            motherListBean.setEducation(mother_education);
            motherListBean.setOccupation(mother_occupation);
            motherListBean.setRelationship(student_mother_relationship);
            motherListBean.setQq(mother_qq);
            contactlistBeenList.add(motherListBean);
        }else {}

        if (!TextUtils.isEmpty(father_relationship)) {
            SignUpSubmitInfoRequest.ContactlistBean fatherListBean = new SignUpSubmitInfoRequest.ContactlistBean();
            fatherListBean.setName(father_name);
            fatherListBean.setSex(fatherSex);
            fatherListBean.setPhone(father_phone);
            fatherListBean.setCardid(father_cardid);
            fatherListBean.setEducation(father_education);
            fatherListBean.setOccupation(father_occupation);
            fatherListBean.setRelationship(father_relationship);
            fatherListBean.setQq(father_qq);

            contactlistBeenList.add(fatherListBean);
        }else {}

        SignUpSubmitInfoRequest.StudentEnrolBean studentEnrolBean = new SignUpSubmitInfoRequest.StudentEnrolBean();
        studentEnrolBean.setName(student_name);
        studentEnrolBean.setCardid(student_cardid);
        studentEnrolBean.setBirthdatetime(student_birthdatetime);
        studentEnrolBean.setCountryid(student_cityid);
        studentEnrolBean.setParity(student_parity);
        studentEnrolBean.setSex(student_sex);
        HashMap<String, String> singleChoiceIdList = evaluateSheetAdapter.getSingleChoiceIdList();
        List<SignUpSubmitInfoRequest.EvaluatelistBean> evaluatelist = new ArrayList<>();

        for (String key : singleChoiceIdList.keySet()) {
            SignUpSubmitInfoRequest.EvaluatelistBean evaluatelistBean = new SignUpSubmitInfoRequest.EvaluatelistBean();
            evaluatelistBean.setId(singleChoiceIdList.get(key));
            evaluatelist.add(evaluatelistBean);
        }
        SignUpSubmitInfoRequest signUpSubmitInfoRequest = new SignUpSubmitInfoRequest();
        signUpSubmitInfoRequest.setAddress(address);//地址
        signUpSubmitInfoRequest.setEmail(email);//邮箱
        signUpSubmitInfoRequest.setPhone(phone);//电话号码
        signUpSubmitInfoRequest.setContactlist(contactlistBeenList);//监护人信息
        signUpSubmitInfoRequest.setStudentEnrol(studentEnrolBean);//学生信息
        signUpSubmitInfoRequest.setEvaluatelist(evaluatelist);//测评表
        showProgressDialog();
        Call<SignUpSubmitInfoResponse> submitInfoCall = MFPApplication.getInstance().getRestClient().signUpSubmitInfo(departid, signUpSubmitInfoRequest);
        submitInfoCall.enqueue(new Callback<SignUpSubmitInfoResponse>() {
            @Override
            public void onResponse(Call<SignUpSubmitInfoResponse> call, Response<SignUpSubmitInfoResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getData() != null) {
                        orderflag = response.body().getData().getOrderflag();
                        final int count = response.body().getData().getCount();
                        PreferencesStorageUtil.setEmail(SignUpEvalucateSheetActivity.this,email);
                        /**
                         * 0 不排名
                         * 1 排名
                         * */
                        if (orderflag.equals("0")) {
                            showHiteLineUpLayoutDialog("关闭", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mSignUpSeccessDialogFragment.dismiss();
                                    OrganizationActivity.startActivity(SignUpEvalucateSheetActivity.this);
                                }
                            });
                        } else {
                            showSingleButtonDialog("关闭",  String.valueOf(count),new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    mSignUpSeccessDialogFragment.dismiss();
                                    OrganizationActivity.startActivity(SignUpEvalucateSheetActivity.this);
                                }
                            });
                        }
                    } else {
                        toast(response.body().getMsg());
                    }
                }else{
                    toast("提交失败");
                }

            }

            @Override
            public void onFailure(Call<SignUpSubmitInfoResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交失败");
            }
        });
    }
}

